package com.yungu.swift.order.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.order.dao.OrderAlarmMapper;
import com.yungu.swift.order.model.dto.OrderAlarmDto;
import com.yungu.swift.order.service.OrderAlarmService;
import com.yungu.swift.socket.push.model.PushContent;
import com.yungu.swift.socket.push.service.PushService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * OrderAlarmServiceImpl
 **/
@Service
public class OrderAlarmServiceImpl extends BaseServiceImpl<OrderAlarmDto> implements OrderAlarmService {
    @Autowired
    private OrderAlarmMapper orderAlarmMapper;

    @Override
    protected IMapper<OrderAlarmDto> getMapper() {
        return orderAlarmMapper;
    }

    @Reference
    private PushService pushService;

    @Override
    public ResponseData<Boolean> addAlarm(String orderId, String appid, String alarmLng, String alarmLat) {
        OrderAlarmDto alarm = orderAlarmMapper.getOrderInfo4Alarm(orderId);
        if (alarm != null) {
            alarm.setCreateOn(new Date());
            alarm.setAppid(appid);
            alarm.setUuid(StringUtils.buildUUID());
            alarm.setAlarmLat(alarmLat);
            alarm.setAlarmLng(alarmLng);

            Map<String, Object> dataMap = MapUtils.build();
            dataMap.put("report", "平台收到乘客报警信息，请马上处理！");
            PushContent pushContentByAssignedOrder = PushContent.newInstance4Admin(appid, PushContent.ALARM_MESSAGE_NOTICE, dataMap);
            pushService.sendMessageByAdmin(pushContentByAssignedOrder);

            return ResponseData.buildSuccessResponse("报警成功", orderAlarmMapper.add(alarm) > 0);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "该订单不存在");
    }
}